package com.gxa.ehome.controller;

import com.gxa.ehome.pojo.SysUser;
import com.gxa.ehome.util.MapUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Map;

// 类中和方法中都指定了method,那么对于register方法来说,两种方式都会有效
//建议: 不在类中声明method

/*基于post

/sysUser/get/1  @PathVariable  Restful

/sysUser/reg post/put
    后:@RequestBody SysUser sysUser
    前: contentType:application/json;charset=UTF-8
         JSON.stringify(obj.field) :   obj.field:json对象

/sysUser/login
    后: @RequestParam("jobNumber") String jobNumber
    前: obj.field*/

@Controller
@RequestMapping(value = "/sysUser")
public class SysUserController {


    @PostMapping(value = "/login")
    @ResponseBody
    public Map<String, Object> login(@RequestParam("jobNumber") String jobNum, @RequestParam("password") String password) throws IOException {
        System.out.println("jobNumber:"+jobNum);
        System.out.println("password:"+password);


        // 如果登录验证不通过,抛出业务异常
        if(1==1){
           // throw new BusinessException(ErrorCode.LOGIN_FAILED);
            throw new IOException("发生了未知的系统异常");
        }

        MapUtil mapUtil = new MapUtil();
        mapUtil.setCode("0");
        mapUtil.setMsg("登录成功");

        return mapUtil.getValues();
    }


    @PostMapping(value = "/login2")
    @ResponseBody
    public Map<String, Object> login(@RequestBody SysUser sysUser){
        System.out.println("sysUser.jobNumber:"+sysUser.getJobNumber());
        System.out.println("sysUser.password:"+sysUser.getPassword());

        MapUtil mapUtil = new MapUtil();
        mapUtil.setCode("0");
        mapUtil.setMsg("登录成功");

        return mapUtil.getValues();
    }

    @PostMapping(value = "/login3")
    @ResponseBody
    public Map<String, Object> login(@RequestBody Map<String,Object> map){

        System.out.println("map:"+map);

        MapUtil mapUtil = new MapUtil();
        mapUtil.setCode("0");
        mapUtil.setMsg("登录成功");

        return mapUtil.getValues();
    }

}
